package com.xzh.service.impl;

import com.xzh.dao.AppointmentAMapper;
import com.xzh.model.Appointment;
import com.xzh.model.search.AppointmentDao;
import com.xzh.service.AppointmentService;
import com.xzh.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.time.LocalTime.now;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    private AppointmentAMapper appointmentsMapper;

    @Autowired
    public AppointmentServiceImpl(AppointmentAMapper appointmentsMapper) {
        this.appointmentsMapper = appointmentsMapper;
    }

    public Map<String, Object> getAppointmentsWithPagination(int page, int size, AppointmentDao appointment) {
        int offset = (page - 1) * size;

        List<AppointmentDao> records;
        int total;

        // 判断是否有查询条件
        if (hasSearchCriteria(appointment)) {
            // 有条件查询
            records = appointmentsMapper.selectListByCondition(appointment, offset, size);
            total = appointmentsMapper.selectCountByCondition(appointment);
        } else {
            // 无条件查询（原来的逻辑）
            records = appointmentsMapper.selectListByPage(offset, size);
            total = appointmentsMapper.selectCount();
        }

        // 构造返回结果
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        data.put("records", records);
        data.put("total", total);

        result.put("success", true);
        result.put("data", data);

        return result;
    }

    // 辅助方法：判断是否有查询条件
    private boolean hasSearchCriteria(AppointmentDao appointment) {
        if (appointment == null) {
            return false;
        }
        // 检查所有可能的查询条件
        return appointment.getAppointmentId() != null ||
                appointment.getStudentId() != null ||
                appointment.getScheduleId() != null ||
                appointment.getCourseId() != null ||
                (appointment.getAppointmentStatus() != null && !appointment.getAppointmentStatus().isEmpty());
    }



    @Override
    public JsonResult<?> addList(AppointmentDao appointmentDao) {
        int studentId = appointmentDao.getStudentId();
        int scheduleId = appointmentDao.getScheduleId();
        Integer count = appointmentsMapper.selectId(studentId, scheduleId);
        if(count != null && count != 0){
            return JsonResult.fail("该学生已预约过该课程");
        } else {
            int courseId = appointmentsMapper.selectCourseId(scheduleId);
            // 设置预约时间为当前时间
            appointmentDao.setAppointmentTime(new Date());
            appointmentDao.setAppointmentStatus("已预约");

            Appointment appointment = new Appointment(
                    appointmentDao.getStudentId(),
                    appointmentDao.getScheduleId(),
                    courseId,
                    appointmentDao.getAppointmentTime(),
                    appointmentDao.getAppointmentStatus()
            );

            boolean result = appointmentsMapper.insert(appointment);
            if(result){
                return JsonResult.success();
            }else {
                return JsonResult.fail("创建失败");
            }
        }
    }

    @Override
    public boolean update(AppointmentDao appointmentDao) {
       boolean result = appointmentsMapper.update(appointmentDao);
       if(result){
           int count = appointmentsMapper.selectCourseId(appointmentDao.getScheduleId());
           appointmentDao.setCourseId(count);
           return appointmentsMapper.updateCourseId(appointmentDao);
       }else {
           return false;
       }
    }

    @Override
    public boolean complete(Integer id) {
        return appointmentsMapper.complete(id);
    }

    @Override
    public boolean confirm(Integer id) {
        return appointmentsMapper.confirm(id);
    }

    @Override
    public boolean cancel(Integer id) {
        return appointmentsMapper.cancel(id);
    }

    @Override
    public boolean reorder(Integer id) {
        return appointmentsMapper.reorder(id);
    }


}
